<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class ProductController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");

	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
              
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			
			$map['pro_name'] = array('LIKE','%'.$sh_q.'%');
			
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}

	  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------
                if( I('shop_mid')){
                       $where['mid'] =  I('shop_mid');
                }
             
		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop_product')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
		
		foreach( $list as $key => $item){
		    $list[$key]['shop_name'] = M('shop')->where('mid="'.$item['mid'].'"')->getField('shop_name');
	            $list[$key]['class_name'] = M('shop_product_class')->where('id="'.$item['class_id'].'"')->getField('class_name');
                    $list[$key]['sale_num'] = M('shop_product_order_pro')->where('pro_id="'.$item['id'].'"')->sum('buy_count');
		}
		
		$this->assign('list',$list);
		$count = M('shop_product')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);	
                $this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);
		$this->display('Product/index');
    }
	
 public function editHome(){
	     $cid = I('cid');
	     $status = I('state');
	     $dataInfo = M('shop_product')->find($cid);
		 
	    if( $dataInfo ){
	       if( $status == 1){
		     $data['is_home'] = 2;
		   }else{
		      $data['is_home'] = 1;
		   }
		   $result = M('shop_product')->where('id="'.$dataInfo['id'].'"')->save($data);
	       }
		 
	     if( $result ){
		 $info = 100;
		 $this->ajaxReturn($info);
		 }
	 
	 }
	 
  public  function edit(){
 
	  $id = I('id');
	  $dataInfo = M('administrator')->where('id="'.$id.'"')->find($id);
	  $typeArr  = array(1=>'一级',2=>'二级');
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->assign('typeArr', $typeArr);
		 $this->display();
	  }

	}
	
  public  function  saveEdit(){
      	 $data['username']= I('username');
         $data['phone'] = I('phone');
         $data['password'] = md5(I('password'));
		 $data['author'] = $_SESSION['admin_user_id'];
		 $data['fullname'] = I('fullname');
		 $data['level'] = I('level');
	     $id = I('updateid');
         if($data){
		  $result = M('administrator')->where('id="'.$id.'"')->save($data);
		 }
		 
		 if($result){
		    $info['status'] =100;
		    $info['info'] = '修改成功';
				  $this->ajaxReturn($info);
		 }
       
	    

  }	
  
  
     public function add(){
		 $typeArr  = array(1=>'一级',2=>'二级');
		 $this->assign('typeArr', $typeArr);
		 $this->display();
    }
	
	
	public  function save(){  
	     $data['username']= I('username');
         $data['phone'] = I('phone');
         $data['password'] = md5(I('password'));
		 $data['author'] = $_SESSION['admin_user_id'];
		 $data['fullname'] = I('fullname');
		 $data['add_time'] = time();
		 $data['level'] = I('level');
		
	     if( $data ){
          $result = M('administrator')->add($data);		 
		 }
	     
		 if( $result ){
		 $info['status'] = 100;
		 $info['info'] = '添加成功';
		 $this->ajaxReturn($info);
		 }else{
		 
		 $info['status'] = 101;
		 $info['info'] = '添加失败';
		 $this->ajaxReturn($info);
		 
		 }
   }
	
	 public function del(){

            $id = $_REQUEST['id'];
			
            if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
			
            $list = M('administrator')->where($where)->delete();
			
			if( $list){
			$data['status'] = 100;
			$this->ajaxReturn($data);
			}
			
			
			
		    //$this->redirect('Banner/index');
		

    }

	
	
	
	 
}